import {Component, OnInit} from '@angular/core';
import {NavigationEnd, Router} from '@angular/router';
import "rxjs/add/operator/filter";
@Component({
    selector: 'app-nav',
    templateUrl: './nav.component.html',
    styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit {
    //这个必须卸载contructor外面,不能在constructor里面声明
    private menus: Array<Menu>;
    private currentPageName: string;

    constructor(private router: Router) {
        router.events.filter(event => event instanceof NavigationEnd)
              .subscribe((event: NavigationEnd) => {
                  if (event.url == '/home') {
                      alert('this is home');
                  }
                  if (event.url == '/product') {
                      alert('this is product');
                  }
              });
    }

    ngOnInit() {
        this.menus = [
            new Menu('home', 'home'),
            new Menu('product', 'product')
        ]
        this.currentPageName = this.menus[0].name;
    }

    pageto(menu: Menu) {
        //console.log(menu);
        this.router.navigateByUrl(menu.routerLink);
        this.currentPageName = menu.name;
    }
}
export class Menu {
    constructor(public name: string,
                public routerLink: string) {
    }
}
